package com.cnvai.cms.web.controller;

import java.math.BigDecimal;
import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cnvai.cms.business.api.ArticlesClassService;
import com.cnvai.cms.business.api.ArticlesService;
import com.cnvai.cms.javabean.model.CmsArticle;
import com.cnvai.cms.javabean.model.CmsArticleclass;
import com.cnvai.cms.javabean.vo.JqueryTree;
import com.cnvai.cms.javabean.vo.NewsBean;
import com.cnvai.cms.web.vo.Datagrid;

@Controller
@RequestMapping("/articles")
public class ArticlesController {

	@Inject
	private ArticlesService articlesService;

	@Inject
	private ArticlesClassService articlesClassService;

	public ArticlesService getArticlesService() {
		return articlesService;
	}

	public void setArticlesService(ArticlesService articlesService) {
		this.articlesService = articlesService;
	}

	public ArticlesClassService getArticlesClassService() {
		return articlesClassService;
	}

	public void setArticlesClassService(ArticlesClassService articlesClassService) {
		this.articlesClassService = articlesClassService;
	}

	@RequestMapping("/articlesClassHome")
	public String articlesClass() {
		return "articles/articlesClass";
	}

	@RequestMapping("/articlesClassList")
	@ResponseBody
	public Datagrid articlesClassList() {
		Datagrid data = new Datagrid();
		List<CmsArticleclass> list = getArticlesClassService().getArticleclassListALL();
		data.setTotal(list.size());
		data.setRows(list);
		return data;
	}

	@RequestMapping("/articlesClassAdd")
	public String articlesClassAdd() {
		return "articles/atriclesClassAdd";
	}

	@RequestMapping("/saveArticlesClass")
	public String saveArticlesClass(CmsArticleclass articlesClass) {
		String result = "success";
		try {
			getArticlesClassService().saveArticleClass(articlesClass);
		} catch (Exception e) {
			result = "fail";
		}
		return result;
	}

	@RequestMapping("/deleteArticlesClass")
	@ResponseBody
	public String deleteArticlesClass(String ids) {
		String result = "success";
		try {
			getArticlesClassService().deleteArticleclass(ids);
		} catch (Exception e) {
			result = "fail";
		}
		return result;
	}

	@RequestMapping("/poparticleclass")
	public String articleclassPop() {
		return "/articles/poparticlesclass";
	}

	@RequestMapping("/validArticleclassList")
	@ResponseBody
	public Datagrid validArticleclassList() {
		Datagrid data = new Datagrid();
		List<CmsArticleclass> list = getArticlesClassService().getValidCmsArticleclassList();
		data.setTotal(list.size());
		data.setRows(list);
		return data;
	}

	@RequestMapping("/articlesHome")
	public String articles() {
		return "articles/articles";
	}

	@RequestMapping("/articlesAdd")
	public String articlesAdd() {
		return "articles/articlesAdd";
	}

	@RequestMapping("/saveArticles")
	public String saveArticles(CmsArticle articles) {
		String result = "success";
		try {
			getArticlesService().saveArticle(articles);
		} catch (Exception e) {
			result = "fail";
		}
		return result;
	}

	@RequestMapping("/articleList")
	@ResponseBody
	public Datagrid articleList(BigDecimal arsId, int pageNumber, int pageSize, String querybuilder) {
		Datagrid data = new Datagrid();
		int total = getArticlesService().totalArticleList(arsId, querybuilder);
		List<NewsBean> list = getArticlesService().pageArticleList(pageNumber, pageSize, arsId, querybuilder);
		data.setTotal(total);
		data.setRows(list);
		return data;
	}

	@RequestMapping("/articlesClassTree")
	@ResponseBody
	public List<JqueryTree> loadArticlesClassTree(BigDecimal id) {
		return getArticlesClassService().getJqueryTreeList(id);
	}
}
